package backend

import (
	"chatgpt/controller/father"
	"chatgpt/helper"
	"chatgpt/helper/code"
	"chatgpt/model/app"
	"chatgpt/services"
	"github.com/gin-gonic/gin"
	"github.com/slclub/go-tips"
)

// -----------------------Product-------------------
// 商品新增修改
// 权益绑定也可以直接用save 处理
func ProductSaveHandle(ctx *gin.Context) {
	data, _ := ctx.Get("RawAny")
	raw, ok := data.(map[string]any)
	if !ok {
		helper.Count(ctx, code.REQ_PARAM_ERR, nil)
		return
	}
	proc_id := father.RawAny(ctx, "ProductID")
	proc := services.ProductSrv.First(proc_id)
	if proc == nil {
		proc = &app.Product{}
	}

	proc.ConvFromMap(raw)
	mc := services.ProductSrv.Save(proc)
	helper.Count(ctx, mc, proc)
}

// 商品列表
func ProductListHandle(ctx *gin.Context) {
	datas := services.ProductSrv.GetList(ctx.GetString("UUID"), 0, 0)
	helper.Count(ctx, code.SUCCESS, datas)
}

// 商品上架 下架等处理
func ProductStatusHandle(ctx *gin.Context) {

}

// -----------------------Price-------------------
// 商品基础价格列表
func PriceBaseListHandle(ctx *gin.Context) {

}

// 商品活动价格列表
func PriceActivityListHandle(ctx *gin.Context) {

}

// 价格表存储
func PriceSaveHandle(ctx *gin.Context) {
	data, _ := ctx.Get("RawAny")
	raw, ok := data.(map[string]any)
	if !ok {
		helper.Count(ctx, code.REQ_PARAM_ERR, nil)
		return
	}
	price := app.Price{}
	price.ConvFromMap(raw)
	mc := services.PriceSrv.Save(&price)
	helper.Count(ctx, mc, nil)
}

// 价格表存储
func PriceDeleteHandle(ctx *gin.Context) {
	id := father.RawAny(ctx, "ID")
	if id == "" {
		helper.Count(ctx, code.REQ_PARAM_ERR, nil)
		return
	}

	mc := services.PriceSrv.Delete(tips.Int(id))
	helper.Count(ctx, mc, nil)
}
